package com.emellend.api.gallery;

import com.emellend.api.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

public class Gallery1 extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_1);
		init();
	}

	private void init() {
		Gallery gallery = (Gallery) this.findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
			}
		});
		registerForContextMenu(gallery);
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		menu.add("Testing");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		Toast.makeText(this, "Longpress: " + menuInfo.position, Toast.LENGTH_SHORT).show();
		return true;
	}

	public class ImageAdapter extends BaseAdapter{
		private static final int ITEM_WIDTH = 136;
        private static final int ITEM_HEIGHT = 88;
        private final int mGalleryItemBackground;
        private final float mDensity;
        private final Context mContext;
        
        private final Integer[] mImageIds = {
                R.drawable.gallery_photo_1,
                R.drawable.gallery_photo_2,
                R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,
                R.drawable.gallery_photo_5,
                R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_7,
                R.drawable.gallery_photo_8
        };
        
        public ImageAdapter(Context context){
        	mContext=context;
        	TypedArray a = context.obtainStyledAttributes(R.styleable.Gallery1);
        	mGalleryItemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        	a.recycle();
        	mDensity=context.getResources().getDisplayMetrics().density;
        }
        
		public int getCount() {
			return mImageIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView==null) {
				convertView=new ImageView(mContext);
				imageView=(ImageView) convertView;
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams((int)(ITEM_WIDTH*mDensity+0.5f),
						(int)(ITEM_HEIGHT*mDensity+0.5f)));
				imageView.setBackgroundResource(mGalleryItemBackground);
			}else {
				imageView=(ImageView) convertView;
			}
			imageView.setImageResource(mImageIds[position]);
			return imageView;
		}
		
	}
}
